<?php

namespace app\common\library;

use fast\Http;
use think\Cache;
use think\Db;

class Jcc
{
    //获取金铲铲房间查询的session_id
    public static function session()
    {
        if (Cache::has("jcc_session")) {
            $session = Cache::get("jcc_session");
        } else {
            $third_url = Db::name('config')->where(['name' => 'third_url'])->value("value", "");

            $reult = Http::post($third_url . "GetMiniAppCode", '{"appid":"wxc3f2d27d4eb91269"}');//获取code
            $array = json_decode($reult, true);
            $session_json = [
                "code" => $array['GetMiniAppCode'],
                "need_openid" => true,
                'weapp_type' => 1
            ];

            $session_result = Http::post("https://minigame.guangzi.qq.com/starweb/login", json_encode($session_json), [
                CURLOPT_HTTPHEADER => [
                    "Referer: https://servicewechat.com/wxc3f2d27d4eb91269/27/page-frame.html"
                ]
            ]);

            $session_info = json_decode($session_result, true);
            if (isset($session_info['ret']) && $session_info['ret'] == 0) {
                Cache::set("jcc_session", $session_info['data']['session_id'], $session_info['data']['expire_seconds'] - 30);
                $session = $session_info['data']['session_id'];
            } else {
                $session = NULL;
            }
        }

        return $session;
    }

    public static function send_msg($wxid, $msg, $teamId)
    {
        $third_url = Db::name('config')->where(['name' => 'third_url'])->value("value", "");
        $third_url_array = explode(",", $third_url);

//        if (Cache::has('url_index')) {
//            $index = Cache::get('url_index');
//        } else {
//            $index = 0;
//        }
        $result = Http::post($third_url_array[1] . "SendTextMsg", json_encode(["wxid" => $wxid, "msg" => $msg]));
        Db::name('third_notinfo')->insert([
            'formg_id' => $wxid,
            'team_id' => $teamId,
            'info' => $result,
            'createtime' => date("Y-m-d H:i:s")
        ]);
        return $result;
    }

    public static function session_id($refresh)
    {
        $third_url = Db::name('config')->where(['name' => 'third_url'])->value("value", "");
        $third_url_array = explode(",", $third_url);

        $count = count($third_url_array);
        $new_count = $count - 1;
        if (Cache::has('url_index')) {//是否缓存链接索引
            $index = Cache::get('url_index');
            if ($refresh) {//是否强制刷新
                $now = time();
                if (Cache::has('refresh_url_time')) {//是否缓存更换链接时间
                    $time = Cache::get('refresh_url_time');
                } else {
                    $time = 0;
                    Cache::set('refresh_url_time', $now);
                }

                $interval = $now - $time;
                if ($interval > 21600 || $interval == $now) {//间隔超过xx秒或初始时间
                    if ($index == $new_count) {
                        $index = 0;
                    } else {
//                        $index = 0;
                        Cache::rm("jcc_session_id");//删除缓存.更换链接
//                        $index = $index + 1;//换IP
                    }
                }
            }
        } else {
            $index = 0;
            Cache::set('url_index', $index);
        }


        if (Cache::has("jcc_session_id")) {
            $session = Cache::get("jcc_session_id");
        } else {
            $session = self::get_session($third_url_array[$index]);
            Cache::set("jcc_session_id", $session, 216000);//两天半
        }
        return $session;
    }

    public static function get_session($url)
    {
        $reult = Http::post($url . "GetMiniAppCode", '{"appid":"wxc3f2d27d4eb91269"}');//获取code
        $array = json_decode($reult, true);
        $session_json = [
            "code" => $array['GetMiniAppCode'],
            "need_openid" => true,
            'weapp_type' => 1
        ];

        $session_result = Http::post("https://minigame.guangzi.qq.com/starweb/login", json_encode($session_json), [
            CURLOPT_HTTPHEADER => [
                "Referer: https://servicewechat.com/wxc3f2d27d4eb91269/27/page-frame.html"
            ]
        ]);

        $session_info = json_decode($session_result, true);
        if (isset($session_info['ret']) && $session_info['ret'] == 0) {
//            Cache::set("jcc_session", $session_info['data']['session_id'], $session_info['data']['expire_seconds'] - 30);
            $session = $session_info['data']['session_id'];
        } else {
            $session = NULL;
        }
        return $session;
    }
}